<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        // 1.浅拷贝
        let obj = {
            id:1,
            name:"zhubin",
            arr:[1,2,3]
        };

        let newObj1 = {};
        for(let key in obj){
            newObj1[key]  = obj[key];
        };
        console.log(newObj1);

        // 2.深拷贝
        let obj2 = {
            id:1,
            name:"zhubin",
            arr:[1,2,["hahaha","hehehehe"],3],
            ob:{
                a:123,
                fn:function(){}
            }
        };

        let childObj = [];
        let a = deepClone(childObj,obj2);
        function deepClone(newObj,oldObj) {
            for (let k in oldObj){
                if(oldObj[k] instanceof Array){
                    newObj[k] = [];
                    deepClone( newObj[k],oldObj[k]);
                }else if(obj[k] instanceof Object){
                    newObj[k]={};
                    deepClone( newObj[k],oldObj[k]);
                }else{
                newObj[k]=oldObj[k];
                };
            };
            return newObj;
        };

        console.log(obj2);
        console.log(childObj);
        console.log(a);
    </script>
</body>
</html>